package com.management.demo.common.dto;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.management.demo.common.vo.PageVO;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

import java.util.List;
import java.util.function.Function;

@Data
@Schema(description = "分页查询参数")
public class PageDTO {

    @NotNull(message = "当前页不能为空")
    @Schema(description = "当前页")
    private Integer currentPage;

    @NotNull(message = "每页显示条数不能为空")
    @Schema(description = "每页显示条数")
    private Integer pageSize;

    /**
     * 根据查询到的分页数据构建返回数据
     * T：原始的分页数据
     * R：封装处理好的分页数据
     */
    public static <T, R> PageVO<R> getPageVO(Page<T> page, Function<T, R> converter) {
        PageVO<R> pageVO = new PageVO<>();
        pageVO.setSize(page.getSize());
        pageVO.setCurrent(page.getCurrent());
        pageVO.setTotal(page.getTotal());
        List<R> records = page.getRecords().stream().map(converter).toList();
        pageVO.setRecords(records);
        return pageVO;
    }
}
